<template>
    <view>
        <u-swipe-action :index="index" v-for="(item, index) in sysUserThirdLoginList" :key="item.id"
                        @click="deleteAuthorization(item, index)" :options="options">
            <view class="item u-border-bottom">
                <div style="padding: 15px; font-size: 16px;">
                    {{item.name}}
                    <u-tag type="error" plain v-if="isAuthorized" :text="'当前微信号'" mode="plain" size="mini" style="margin-left: 10px;"></u-tag>
                </div>
            </view>
        </u-swipe-action>

        <div style="padding-top: 50px;" v-if="!sysUserThirdLoginList.length">
            <u-empty text="数据为空" mode="data"></u-empty>
        </div>

        <div class="custom-bottom-button">
            <label v-if="!isAuthorized" @click="showAuthorizationModel = true">
                点击授权登录
            </label>
            <label v-if="isAuthorized" style="background-color: #eeeeee; color: #565354;">
                当前用户已授权
            </label>
        </div>

        <u-modal v-model="showAuthorizationModel" :title-style="{color: 'red'}" :show-cancel-button="true" @cancel="showAuthorizationModel = false"
                 @confirm="authorization()">
            <view class="slot-content">
                <u-field
                        :label-width="200"
                        v-model="name"
                        label="授权用户名称"
                        placeholder="请输入授权用户名称便于识别。"
                        :error-message="nameErrorMessage"
                >
                </u-field>
            </view>
        </u-modal>
    </view>
</template>

<script>

    import {SysUserThirdLoginService} from "@/dev/services/sys_user_third_login.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";
    import {WechatService} from "@/dev/services/wechat.service";

    export default {
        data() {
            return {
                isAuthorized: false,  // 当前用户是否已授权
                sysUserThirdLoginList: [],
                // 侧滑菜单
                options: [
                    {
                        text: '删除',
                        style: {
                            backgroundColor: '#dd524d'
                        }
                    }
                ],
                showAuthorizationModel: false,
                name: '',
                nameErrorMessage: false,
                loginId: ''
            }
        },
        onReady() {
            const _self = this;
            uni.setNavigationBarTitle({
                title: `${_self.urlParams.title}已授权用户`
            });

            new SysUserThirdLoginService().findSysUserThirdLoginList({
                userId: _self.globalVariable.userInfo.id,
            }).then(result => {
                _self.sysUserThirdLoginList = result;

                // 获得unionid
                new WechatService().findUnionidByCode().then(result => {
                    _self.loginId = result.data;
                    _self.sysUserThirdLoginList.forEach((item, index) => {
                        if (item.loginId === _self.loginId) {
                            _self.isAuthorized = true;
                        }
                    });
                });
            })
        },
        methods: {
            authorization() {
                const _self = this;
                _self.name = CommonUtil.trim(_self.name);
                if (!_self.name) {
                    _self.nameErrorMessage = "请输入授权用户名称便于识别";
                    _self.showAuthorizationModel = true;
                    return;
                } else {
                    _self.nameErrorMessage = false;
                }

                CommonUtil.alert({
                    title: '提示！',
                    content: `确认授权${_self.urlParams.title}登录吗？`,
                    showCancel: true, // 是否显示取消按钮
                    success: function (res) {
                        if (res.cancel) { // 点击取消,默认隐藏弹框
                        } else { // 点击确定
                            new SysUserThirdLoginService().weixinLoginAuthorizationByUnionid({
                                loginId: _self.loginId,
                                type: _self.globalVariable.runPlatform,
                                name: _self.name
                            }).then(result => {
                                _self.name = '';
                                if (result.status === "success") {
                                    CommonUtil.toast("授权成功");
                                    _self.sysUserThirdLoginList.splice(0, 0, result.data);
                                    _self.sysUserThirdLoginList.forEach((item, index) => {
                                        if (item.loginId === _self.loginId) {
                                            _self.isAuthorized = true;
                                        }
                                    });
                                } else {
                                    CommonUtil.alert(result.message);
                                }
                            })
                        }
                    }
                });
            },
            deleteAuthorization(item, index) {
                const _self = this;
                CommonUtil.alert({
                    title: '提示！',
                    content: `确认移除${item.name}登录授权吗？`,
                    showCancel: true, // 是否显示取消按钮
                    success: function (res) {
                        if (res.cancel) { // 点击取消,默认隐藏弹框
                        } else { // 点击确定
                            new SysUserThirdLoginService().deleteSysUserThirdLoginByIds(item.id).then(result => {
                                CommonUtil.toast("移除成功");
                                _self.sysUserThirdLoginList.splice(index, 1);
                                _self.isAuthorized = false;
                                _self.sysUserThirdLoginList.forEach((item, index) => {
                                    if (item.loginId === _self.loginId) {
                                        _self.isAuthorized = true;
                                    }
                                })
                            })
                        }
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
